﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace DIS_App_Client.MainManagers
{
	#region using
	using DIS_App_Client.Helpers;
	using System.Data;
	using System.Drawing;
	using System.IO;
	using DIS_App_Client.DIS_WS_BusinessLayer;
    using DIS_App_Client.Reports;
	#endregion
	public partial class FrmManageOrders : Form
	{
		public EventHandler Handler { get; set; }
        private DataTable DataSource;

		public FrmManageOrders()
		{
			InitializeComponent();
		}

		private void FrmManageOrders_Load(object sender, EventArgs e)
		{
			InitGridView();
			LoadData();
			dtgvData.ClearSelection();
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			FrmCreateOrders frm = new FrmCreateOrders();
			frm.Handler += LoadData;
			frm.ShowDialog();
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			if (dtgvData.SelectedRows.Count > 0)
			{
                if (MessageBox.Show(CommonHelper.GetMessage("Delete", "Confirm"), "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
				{
					if (BusService.Instance.Orders_Delete(int.Parse(dtgvData.SelectedRows[0].Cells["ID"].Value.ToString())))
					{
                        MessageBox.Show(CommonHelper.GetMessage("Delete", "Successful"), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
						LoadData();
					}
					else
					{
                        MessageBox.Show(CommonHelper.GetMessage("Delete", "Fail"), "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
					}
				}
			}
		}

		private void btnUpdate_Click(object sender, EventArgs e)
		{
			if (dtgvData.SelectedRows.Count > 0)
			{
				FrmCreateOrders frm = new FrmCreateOrders(int.Parse(dtgvData.SelectedRows[0].Cells["ID"].Value.ToString()));
				frm.Handler += LoadData;
				frm.ShowDialog();
			}
		}

		private void btnExit_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void InitGridView()
		{
			DataGridViewTextBoxColumn objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "ID",
				DataPropertyName = "ID",
				Name = "ID",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/6))
			};
			dtgvData.Columns.Add(objColumn);
			objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "Chemist WareHouse",
                DataPropertyName = "ChemistWareHouse",
                Name = "ChemistWareHouse",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/6))
			};
			dtgvData.Columns.Add(objColumn);
			objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "OrderDate",
				DataPropertyName = "OrderDate",
				Name = "OrderDate",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/6))
			};
			dtgvData.Columns.Add(objColumn);
			objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "IsVerified",
                DataPropertyName = "IsVerified",
                Name = "IsVerified",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/6))
			};
			dtgvData.Columns.Add(objColumn);
			objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "IsShipped",
                DataPropertyName = "IsShipped",
                Name = "IsShipped",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/6))
			};
			dtgvData.Columns.Add(objColumn);
            objColumn = new DataGridViewTextBoxColumn
            {
                HeaderText = "IsPaid",
                DataPropertyName = "IsPaid",
                Name = "IsPaid",
                Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth) * ((double)1 / 6)) - 2
            };
            dtgvData.Columns.Add(objColumn);
		}

		private void LoadData(object sender, EventArgs e)
		{
			LoadData();
		}

		private void LoadData()
		{
			var lstItem = BusService.Instance.Orders_SelectByIDChemistWareHouse(GlobalVariables.g_CurrentUser.IDChemistWareHouse);
			var dt = new DataTable();
			dt.Columns.Add("ID");
            dt.Columns.Add("ChemistWareHouse");
			dt.Columns.Add("OrderDate");
			dt.Columns.Add("IsVerified");
            dt.Columns.Add("IsShipped");
			dt.Columns.Add("IsPaid");
			for (int i = 0; i < lstItem.Length; i++)
			{
				dt.Rows.Add(lstItem[i].ID, lstItem[i].ChemistWareHouse.Name, String.Format("{0:dd/MM/yyyy}", lstItem[i].OrderDate), lstItem[i].IsVerified == true ? "Yes" : "No", lstItem[i].IsShipped == true ? "Yes" : "No", lstItem[i].IsPaid == true ? "Yes" : "No");
			}
			DataView objTimeSheetDV = new System.Data.DataView(dt);
			objTimeSheetDV.AllowNew = false;
			dtgvData.AutoGenerateColumns = false;
			dtgvData.DataSource = objTimeSheetDV;
			dtgvData.ClearSelection();
		}

        private void dtgvData_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dtgvData.SelectedRows.Count > 0)
            {
                FrmCreateOrders frm = new FrmCreateOrders(int.Parse(dtgvData.SelectedRows[0].Cells["ID"].Value.ToString()));
                frm.Handler += LoadData;
                frm.ShowDialog();
            }
        }

        public void InitDataSource()
        {
            
        }

        private void btnPrintOrder_Click(object sender, EventArgs e)
        {
            if (dtgvData.SelectedRows.Count <= 0)
            {
                MessageBox.Show("Order is empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            var idOrder = int.Parse(dtgvData.SelectedRows[0].Cells["ID"].Value.ToString());
            var lst = BusService.Instance.OrderDetail_SelectByIDOrder(idOrder);
            var sum = 0.0;
            if (lst.Length > 0)
            {
                DataSource = new DataTable();
                DataSource.Columns.Add("SeqNum");
                DataSource.Columns.Add("IDItem");
                DataSource.Columns.Add("Item");
                DataSource.Columns.Add("Quantity");
                DataSource.Columns.Add("Price");
                DataSource.Columns.Add("Discount");

                var seqnum = 0;
                foreach (var item in lst)
                {
                    seqnum++;
                    var money = item.Quantity * item.Price;
                    var remain = money - money * (item.Discount / 100);
                    sum += remain;
                    DataSource.Rows.Add(seqnum, item.IDItem, item.Item.Name, item.Quantity, item.Price, item.Discount);
                }          
            }

            FrmPrintOrder frm = new FrmPrintOrder
            {
                IDOrder = idOrder.ToString(),
                OrderDate = DateTime.Now.ToString(),
                Sum = Math.Round(sum, 2).ToString() + " USD",
                DataSource = DataSource
            };
            frm.ShowDialog();
        }

	}
}